package com.itheima.test;

import java.util.Scanner;

public class test1case1 {
    public static void main(String[] args) {
        //键盘录入字符串，要求长度《=9，只能是数字
        //将内容变成罗马数字

        //键盘录入
        Scanner sc = new Scanner(System.in);
        //String str = "12345a";
        String str;
        while (true) {
            System.out.println("请输入一个字符串：");
            str = sc.next();

            //校验是否满足要求
            boolean flag = checkStr(str);
            if(flag){
                break;
            }else{
                System.out.println("字符串不满足要求,，请重新输入");
                continue;
            }
        }

        //将字符串变成罗马数字，查表法
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            //System.out.println(c);
            int number = c-48;//number为对应数字
            String s = getRoman(number);
            //System.out.println(s);
            //拼接
            sb.append(s);
        }

        System.out.println(sb.toString());


    }

    //查表法
    public static String getRoman(int num) {
        String[] arr = {"","I","II","III","IV","V","VI","VII","VIII","IX"};//注意0索引对应“”
        return arr[num];
    }

    public static boolean checkStr(String str) {
        //要求1：长度<=9
        if(str.length()>9){
            return false;
        }

        //要求2：只能是数字
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch<'0' || ch>'9'){
                return false;
            }
        }
        return true;
    }
}
